package com.vgemv.jsutilitysdk.activity.ChooseApp;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.R;


public class JsAppItemView extends JsMenuItemView {

    private RelativeLayout mRl_root;

    ImageView mIv;
    TextView mTv_appName;
    TextView mTv_packageName;


    public JsAppItemView(Context context) {
        super(context);
        {
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mRl_root = new RelativeLayout(context);
            addView(mRl_root, lp);
//            mRl_root.setBackgroundColor(Color.RED);
        }
        {
            mIv = new ImageView(context);
            mIv.setId(View.generateViewId());
            int size = JsGeometryManager.pixelFromDp(50);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(size,size);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            lp.setMargins(JsGeometryManager.pixelFromDp(3),0,0,0);
            mRl_root.addView(mIv,lp);
        }
        {
            mTv_appName = new TextView(context);
            mTv_appName.setId(View.generateViewId());
            mTv_appName.setTextSize(18);
            //android中为textview动态设置字体为粗体
            mTv_appName.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.RIGHT_OF,mIv.getId());
            lp.addRule(RelativeLayout.ALIGN_TOP,mIv.getId());
            lp.setMargins(JsGeometryManager.pixelFromDp(10),JsGeometryManager.pixelFromDp(0),0,0);
            mRl_root.addView(mTv_appName,lp);
        }
        {
            mTv_packageName = new TextView(context);
            mTv_packageName.setId(View.generateViewId());
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.RIGHT_OF,mIv.getId());
            lp.addRule(RelativeLayout.BELOW,mTv_appName.getId());
            lp.setMargins(JsGeometryManager.pixelFromDp(10),JsGeometryManager.pixelFromDp(0),0,0);
            mRl_root.addView(mTv_packageName,lp);
        }
    }

    @Override
    public void customSetVm(Context context, JsMenuItemVm itemVm) {
        JsAppItemModel jsAppItemVm = (JsAppItemModel)itemVm.currentModel();
        mTv_packageName.setText(jsAppItemVm.packageName);
        mTv_appName.setText(jsAppItemVm.appName);
//        Drawable defaultIcon = getContext().getDrawable(R.mipmap.ic_launcher);
//        if(jsAppItemVm.drawable_icon == null) {
//            jsAppItemVm.drawable_icon = getAppIcon(getContext(),jsAppItemVm.packageName);
//        }
//        else {
//
//        }
        mIv.setImageDrawable(jsAppItemVm.drawable_icon);
    }

    public Drawable getAppIcon(Context context,String packageName){
        Drawable drawable;
        try{
            drawable = context.getPackageManager().getApplicationIcon(packageName);
        }
        catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
            drawable = context.getDrawable(R.mipmap.ic_launcher);
        }
        return drawable;
    }
}
